package servlet;


import bean.Page;
import bean.user_info;
import com.github.pagehelper.PageInfo;
import dao.UserDao;
import dao.impl.UserDaoImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/InforPerfectServlet")
public class InforPerfectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method=req.getParameter("method");

        UserDao ifp=new UserDaoImpl();
        user_info uf=new user_info();


        if(method.equals("InfoAdd")){
            int uinfoid=Integer.parseInt(req.getParameter("uinfoid"));
            String rname=req.getParameter("rname");
            String gender=req.getParameter("gender");
            String phoneNumber=req.getParameter("phoneNumber");
            String address=req.getParameter("address");
            String inductionTime=req.getParameter("inductionTime");
            Date date=new Date();
            try {
                date=sdf.parse(inductionTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int i=ifp.infoADD(uinfoid,rname,gender,phoneNumber,date,address,0);
            if(i!=0){
                resp.sendRedirect("InforPerfectServlet?method=InfoFind");
            }
        }else if (method.equals("InfoDelete")){
            int uinfoid=Integer.parseInt(req.getParameter("uinfoid"));
            int i=new UserDaoImpl().DeleteInfo(uinfoid);
            if (i!=0){
                resp.sendRedirect("InforPerfectServlet?method=InfoFind");
            }else {
                resp.sendRedirect("error.jsp");
            }
        }else if (method.equals("InfoUpdateFind")){
            int uinfoid=Integer.parseInt(req.getParameter("uinfoid"));
            user_info user=new UserDaoImpl().InfoUpdateFind(uinfoid);
            req.setAttribute("user",user);
            req.getRequestDispatcher("/InfoPerfectUpdate.jsp").forward(req,resp);
        }else if (method.equals("InfoFind")){
            /*
            * 前端点击员工管理后跳转到这里执行查询员工的信息然后返回到员工页面(staff.jsp)显示
            * 员工的所有信息.
            * */

            PageInfo list=new UserDaoImpl().InfoList(1,3);
            req.setAttribute("Page",list);
            req.getRequestDispatcher("/staff.jsp").forward(req,resp);


        }else if (method.equals("InfoUpdate")){
            System.out.println("888");
            int uinfoid=Integer.parseInt(req.getParameter("uinfoid"));
            String rname=req.getParameter("rname");
            String gender=req.getParameter("gender");
            String phoneNumber=req.getParameter("phoneNumber");
            String address=req.getParameter("address");
            String inductionTime=req.getParameter("inductionTime");
            double salary= Double.parseDouble(req.getParameter("salary"));
            Date date=new Date();
            try {
                date=sdf.parse(inductionTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int i=new UserDaoImpl().InfoUpdate(uinfoid,rname,gender,phoneNumber,date,address,salary);

        }else if(method.equals("InfoSearch")){
            String rname=req.getParameter("rname");
            if(rname.equals("Search")){

                resp.sendRedirect("InforPerfectServlet?method=InfoFind");
            }else{
                PageInfo user=new UserDaoImpl().InfoSearch(rname);
                req.setAttribute("Page",user);
                req.getRequestDispatcher("/staff.jsp").forward(req,resp);

            }


        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
